

#include "BSP_RFID.h"
#include "IO.h"
#include "rfid_interface.h"
#include "bsp_delay.h"
#include "bsp_config.h"

#include "Log.h"

#if BSP_RFID_VER == BSP_RFID_V2

#define BSP_RFID_ADC_FREQ			RFID_ITF_COLLECT_FREQ
#define BSP_RFID_BUF_LEN			RFID_ITF_DMA_BUF_LEN

/* RFRX : PA12 */
#define RFRX_PORT			GPIOA
#define RFRX_PIN			GPIO_PIN_3

/* RFTX : PA11 */
#define RFTX_PORT			GPIOA
#define RFTX_PIN			GPIO_PIN_4

//RX ADC_IN3
static const IO_T RFID_RxPin = IO_INIT_TABLE(RFRX_PORT, RFRX_PIN);

//TX TIMER0_CH1
#define RFID_TX_AF		GPIO_AF_8
static const IO_T RFID_TxPin = IO_INIT_TABLE(RFTX_PORT, RFTX_PIN);

uint16_t adc_value[2][BSP_RFID_BUF_LEN];

static void tx_timer_config(void);
static void io_config(void);
static void adc_config(void);
static void dma_config(void);
static void rx_timer_config(void);
static void dma_config2(void);


//ID卡硬件初始化
void BSP_RFID_Init(void)
{
#if 1
	io_config();
	adc_config();
	dma_config();
	rx_timer_config();
	tx_timer_config();
//		BSP_RFID_Disable();//默认关闭125Khz射频
	
#endif 
}

#if 1
void BSP_RFID_Enable(void)
{
	timer_disable(TIMER1);
	timer_disable(TIMER0);//停止125KHz射频
	dma_channel_disable(DMA1, DMA_CH0);//停止DMA传输
	dma_config2();
	dma_channel_enable(DMA1, DMA_CH0);//开启DMA传输
	timer_enable(TIMER0);//开启125KHz射频
	timer_enable(TIMER1);
}

void BSP_RFID_Disable(void)
{
	timer_disable(TIMER1);
	timer_disable(TIMER0);//停止125KHz射频
	dma_channel_disable(DMA1, DMA_CH0);//停止DMA传输
}

//125KHz射频配置
#if 1
static void tx_timer_config(void)
{
    /* TIMER0 configuration: generate PWM signals with different duty cycles:
       TIMER0CLK = 180 MHz */
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;	
	
    rcu_periph_clock_enable(RCU_TIMER0);
    timer_deinit(TIMER0);
	
	uint32_t timer_ck = rcu_clock_freq_get(CK_APB2) * 2U;
	//PWM周期125KHz
	uint32_t period =  timer_ck / 125000U;
	
    /* TIMER0 configuration */
    timer_initpara.prescaler         = 1 - 1;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = period;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER0,&timer_initpara);
#if 0
	//ADC触发信号
	 /* CH0 configuration in PWM mode1 */
	timer_channel_output_struct_para_init(&timer_ocintpara);
	timer_ocintpara.ocpolarity  = TIMER_OC_POLARITY_LOW;
	timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
	timer_channel_output_config(TIMER0, TIMER_CH_0, &timer_ocintpara);

	const uint16_t pulse = period * 41U / 100U;
	
	timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, pulse);
	timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM0);
	timer_channel_output_shadow_config(TIMER0, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);
#endif 
	//125K输出信号
	/* CH1 configuration in PWM mode0 */
    timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
    timer_channel_output_config(TIMER0,TIMER_CH_1,&timer_ocintpara);
	
	//输出50%占空比PWM信号
    timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_1, period/2);
    timer_channel_output_mode_config(TIMER0, TIMER_CH_1, TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER0, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);

    timer_primary_output_config(TIMER0,ENABLE);
    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER0);
//	    timer_enable(TIMER0);
}

void bsp_lf_adc_phase(uint16_t pulse){
	timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, pulse);
}

uint32_t bsp_lf_rf_period(void){
	uint32_t timer_ck = rcu_clock_freq_get(CK_APB2) * 2U;
	//PWM周期125KHz
	uint32_t period =  timer_ck / 125000U;
	return period;
}

uint16_t bsp_lf_adc_value(void){
	uint32_t sum = 0;
	for(uint32_t i = 0; i < BSP_RFID_BUF_LEN; i++){
		sum += adc_value[0][i];
	}
	return sum / BSP_RFID_BUF_LEN;
}

#endif 

static void rx_timer_config(void){
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;	
	
    rcu_periph_clock_enable(RCU_TIMER1);
    timer_deinit(TIMER1);
	
	uint32_t timer_ck = rcu_clock_freq_get(CK_APB1) * 2U;
	//PWM周期125KHz
	uint32_t period =  timer_ck / RFID_ITF_COLLECT_FREQ;
	
    /* TIMER0 configuration */
    timer_initpara.prescaler         = 1 - 1;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = period;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER1,&timer_initpara);
	//ADC触发信号
	 /* CH0 configuration in PWM mode1 */
	timer_channel_output_struct_para_init(&timer_ocintpara);
	timer_ocintpara.ocpolarity  = TIMER_OC_POLARITY_LOW;
	timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
	timer_channel_output_config(TIMER1, TIMER_CH_1, &timer_ocintpara);

	const uint16_t pulse = period * 41U / 100U;
	
	timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, pulse);
	timer_channel_output_mode_config(TIMER1, TIMER_CH_1, TIMER_OC_MODE_PWM0);
	timer_channel_output_shadow_config(TIMER1, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
	
    timer_primary_output_config(TIMER1,ENABLE);
    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER1);
}

//引脚初始化
static void io_config(void)
{
	IO_Init(&RFID_RxPin, IO_Mode_AIN);
	
	IO_Init(&RFID_TxPin, IO_Mode_AFPP);
	IO_AF_Set(&RFID_TxPin, RFID_TX_AF);
//		gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_4);
//		gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_166MHZ,GPIO_PIN_4);
//		gpio_af_set(GPIOA, GPIO_AF_8, GPIO_PIN_4);

//		gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_8);
//	    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_166MHZ,GPIO_PIN_8);
//	    gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_8);

}

//接收定时器配置
static void adc_wait_stability(void){
	for(volatile uint32_t i = 0; i < 1800; i++){

	}
}
//接收ADC配置
static void adc_config(void)
{
    /* enable ADC clock */
    rcu_periph_clock_enable(RCU_ADC);
    adc_clock_config(ADC_ADCCK_HCLK_DIV6);

    /* ADC channel length config */
    adc_channel_length_config(ADC_REGULAR_CHANNEL, 1);

    /* ADC regular channel config */
    adc_regular_channel_config(0, ADC_CHANNEL_3, ADC_SAMPLETIME_14POINT5);

    /* ADC external trigger enable */
    adc_external_trigger_config(ADC_REGULAR_CHANNEL, ENABLE);
    /* ADC external trigger source config */
    adc_external_trigger_source_config(ADC_REGULAR_CHANNEL, ADC_EXTTRIG_REGULAR_T1_CH1);
    /* ADC data alignment config */
    adc_data_alignment_config(ADC_DATAALIGN_RIGHT);

    /* enable ADC interface */
    adc_enable();
    /* wait for ADC stability */
//	    delay_1ms(1);
	adc_wait_stability();
    /* ADC DMA function enable */
    adc_dma_request_after_last_enable();
    adc_dma_mode_enable();
    /* enable ADC software trigger */
    adc_software_trigger_enable(ADC_REGULAR_CHANNEL);
}

//接收DMA配置
static void dma_config(void)
{
    /* enable interrupt */
    nvic_irq_enable(DMA1_Channel0_IRQn, 14, 0);

    /* ADC_DMA_channel configuration */
    dma_single_data_parameter_struct dma_single_data_parameter;
    /* enable DMA clock */
    rcu_periph_clock_enable(RCU_DMA1);    
#if 0
    /* ADC DMA_channel configuration */
    dma_deinit(DMA1, DMA_CH0);
    
    /* initialize DMA single data mode */
    dma_single_data_parameter.periph_addr         = (uint32_t)(&ADC_RDATA);
    dma_single_data_parameter.periph_inc          = DMA_PERIPH_INCREASE_DISABLE;
    dma_single_data_parameter.memory0_addr        = (uint32_t)(&adc_value[0][0]);
    dma_single_data_parameter.memory_inc          = DMA_MEMORY_INCREASE_ENABLE;
    dma_single_data_parameter.periph_memory_width = DMA_PERIPH_WIDTH_16BIT;
    dma_single_data_parameter.direction           = DMA_PERIPH_TO_MEMORY;
    dma_single_data_parameter.number              = BSP_RFID_BUF_LEN;
    dma_single_data_parameter.priority            = DMA_PRIORITY_HIGH;
    dma_single_data_mode_init(DMA1, DMA_CH0, &dma_single_data_parameter);
    dma_channel_subperipheral_select(DMA1, DMA_CH0, DMA_SUBPERI0);
	
	dma_switch_buffer_mode_config(DMA1, DMA_CH0, (uint32_t)(&adc_value[1][0]), DMA_MEMORY_0);
	dma_switch_buffer_mode_enable(DMA1, DMA_CH0);
	//双缓冲模式开启后，硬件自动打开循环模式
    /* enable DMA circulation mode */
//	    dma_circulation_enable(DMA1, DMA_CH0);

	dma_interrupt_flag_clear(DMA1, DMA_CH0, DMA_INT_FLAG_FTF|DMA_INTF_SDEIF|DMA_INTF_TAEIF|DMA_INTF_HTFIF);
	dma_interrupt_enable(DMA1, DMA_CH0, DMA_INT_FTF);
#endif 
	dma_config2();
    /* enable DMA channel */
    dma_channel_enable(DMA1, DMA_CH0);

}

static void dma_config2(void)
{
    /* enable interrupt */
//	    nvic_irq_enable(DMA1_Channel0_IRQn, 14, 0);

    /* ADC_DMA_channel configuration */
    dma_single_data_parameter_struct dma_single_data_parameter;
    /* enable DMA clock */
//	    rcu_periph_clock_enable(RCU_DMA1);    
    /* ADC DMA_channel configuration */
    dma_deinit(DMA1, DMA_CH0);
    
    /* initialize DMA single data mode */
    dma_single_data_parameter.periph_addr         = (uint32_t)(&ADC_RDATA);
    dma_single_data_parameter.periph_inc          = DMA_PERIPH_INCREASE_DISABLE;
    dma_single_data_parameter.memory0_addr        = (uint32_t)(&adc_value[0][0]);
    dma_single_data_parameter.memory_inc          = DMA_MEMORY_INCREASE_ENABLE;
    dma_single_data_parameter.periph_memory_width = DMA_PERIPH_WIDTH_16BIT;
    dma_single_data_parameter.direction           = DMA_PERIPH_TO_MEMORY;
    dma_single_data_parameter.number              = BSP_RFID_BUF_LEN;
    dma_single_data_parameter.priority            = DMA_PRIORITY_HIGH;
    dma_single_data_mode_init(DMA1, DMA_CH0, &dma_single_data_parameter);
    dma_channel_subperipheral_select(DMA1, DMA_CH0, DMA_SUBPERI0);
	
	dma_switch_buffer_mode_config(DMA1, DMA_CH0, (uint32_t)(&adc_value[1][0]), DMA_MEMORY_0);
	dma_switch_buffer_mode_enable(DMA1, DMA_CH0);
	//双缓冲模式开启后，硬件自动打开循环模式
    /* enable DMA circulation mode */
//	    dma_circulation_enable(DMA1, DMA_CH0);

	dma_interrupt_flag_clear(DMA1, DMA_CH0, DMA_INT_FLAG_FTF);
	dma_interrupt_flag_clear(DMA1, DMA_CH0, DMA_INTF_SDEIF);
	dma_interrupt_flag_clear(DMA1, DMA_CH0, DMA_INTF_TAEIF);
	dma_interrupt_flag_clear(DMA1, DMA_CH0, DMA_INTF_HTFIF);
	dma_interrupt_flag_clear(DMA1, DMA_CH0, DMA_INT_FLAG_FEE);

	dma_interrupt_enable(DMA1, DMA_CH0, DMA_INT_FTF);
    /* enable DMA channel */
//	    dma_channel_enable(DMA1, DMA_CH0);

}


void DMA1_Channel0_IRQHandler (void)
{
	if(dma_interrupt_flag_get(DMA1, DMA_CH0, DMA_INT_FLAG_FTF))//全完成中断
	{
		dma_interrupt_flag_clear(DMA1, DMA_CH0, DMA_INT_FLAG_FTF);
		uint16_t* last_buf;
		if(dma_using_memory_get(DMA1, DMA_CH0) == DMA_MEMORY_1){
			last_buf = &adc_value[0][0];
		}
		else{
			last_buf = &adc_value[1][0];
		}
		rfid_itf_decode(last_buf, BSP_RFID_BUF_LEN);
	}
}
#endif 

#endif

